package com.example.base.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.excetion.GlobalExcetion;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.example.base.system.domain.OpenUserToken;
import com.example.base.system.mapper.OpenUserTokenMapper;
import com.example.base.system.service.OpenUserTokenService;
import org.springframework.cache.annotation.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;

import java.io.Serializable;
import java.util.Date;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2024-04-21
 */
@Service
@Transactional
@CacheConfig(cacheNames = "OpenUserTokenServiceImpl")
public class OpenUserTokenServiceImpl extends ServiceImpl<OpenUserTokenMapper, OpenUserToken> implements OpenUserTokenService {

    @Cacheable(cacheNames = "openusertoken",key = "#id")
    @Override
    public OpenUserToken getById(Serializable id){
        return super.getById(id);
    }

    @CacheEvict(cacheNames = "openusertoken", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(cacheNames = "openusertoken", key = "#openusertoken.id")
    @Override
    public boolean updateById(OpenUserToken openusertoken) {
        return super.updateById(openusertoken);
    }

    @CachePut(cacheNames = "openusertoken", key = "#openusertoken.id")
    public OpenUserToken saveOpenUserToken(OpenUserToken openusertoken) throws GlobalExcetion {
        super.save(openusertoken);
        return openusertoken;
    }

    @CacheEvict(cacheNames = "openusertoken", key = "#openusertoken.id")
    public OpenUserToken updateOpenUserTokenById(OpenUserToken openusertoken) throws GlobalExcetion{
        super.updateById(openusertoken);
        return openusertoken;
    }

    @Override
    public OpenUserToken valited(String token) {
        LambdaQueryWrapper<OpenUserToken> openUserTokenLambdaQueryWrapper=new LambdaQueryWrapper<>();
        openUserTokenLambdaQueryWrapper.eq(OpenUserToken::getToken,token);
        OpenUserToken openUserToken= this.baseMapper.selectOne(openUserTokenLambdaQueryWrapper);
        if(!openUserToken.getExpiredate().after(new Date())){
            return null;
        }
        return openUserToken;
    }


}
